﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Fadd.Net
{
    /// <summary>
    /// Tries to reconnect a socket
    /// </summary>
    class SocketReconnecter
    {
    	private readonly IPEndPoint _endPoint;
    	private Timer _timer;
        private Socket _socket;

        public SocketReconnecter(TimeSpan retryInterval, IPEndPoint endPoint)
        {
        	_endPoint = endPoint;
        	_timer = new Timer(OnRetry, null, retryInterval, retryInterval);
        }

    	private void OnRetry(object state)
        {
            try
            {
                _socket.Connect(_endPoint);
            }
            catch (SocketException)
            {

            }
            catch (InvalidOperationException)
            {

            }
            catch (Exception err)
            {
                UnhandledException.TriggerEvent(this, "Reconnect attempt failed.", err);
            }
        }
    }

    public class ReconnectedEventArgs : EventArgs
    {
        public ReconnectedEventArgs(Socket socket)
        {
            
        }
    }
}
